package chatclient_01.dialogs;

import chatclient_01.ChatClient;
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import network_01.Network;

public class StyleDialog extends JDialog {

  private static final long serialVersionUID = 1L;
  private Style style = null;
  private Style previousStyle = null;
  private Boolean commit = Boolean.valueOf(false);
  private StyledDocument sample = null;
  private ChatClient chatClient = null;
  private Color initialForeground = null;
  private Color initialBackground = null;
  private JButton OKbutton;
  private JButton backgroundButton;
  private JCheckBox boldCheckBox;
  private JButton cancelButton;
  private JComboBox fontNameComboBox;
  private JComboBox fontSizeComboBox;
  private JButton foregroundButton;
  private JButton helpButton;
  private JCheckBox italicCheckBox;
  private JLabel jLabel3;
  private JLabel jLabel5;
  private JLabel jLabel6;
  private JLabel jLabel7;
  private JPanel jPanel2;
  private JPanel jPanel3;
  private JScrollPane jScrollPane2;
  private JTextPane sampleTextPane;
  private JCheckBox strikeCheckBox;
  private JCheckBox subscriptCheckBox;
  private JCheckBox superscriptCheckBox;
  private JCheckBox underlineCheckBox;

  public StyleDialog(ChatClient parent, boolean modal) {
    super(parent, modal);
    this.chatClient = parent;
    initComponents();

    this.sample = this.sampleTextPane.getStyledDocument();
  }

  private void initComponents() {
    this.jScrollPane2 = new JScrollPane();
    this.sampleTextPane = new JTextPane();
    this.jPanel2 = new JPanel();
    this.fontSizeComboBox = new JComboBox();
    this.fontNameComboBox = new JComboBox();
    this.jLabel5 = new JLabel();
    this.jLabel3 = new JLabel();
    this.boldCheckBox = new JCheckBox();
    this.italicCheckBox = new JCheckBox();
    this.underlineCheckBox = new JCheckBox();
    this.strikeCheckBox = new JCheckBox();
    this.superscriptCheckBox = new JCheckBox();
    this.subscriptCheckBox = new JCheckBox();
    this.jPanel3 = new JPanel();
    this.jLabel6 = new JLabel();
    this.jLabel7 = new JLabel();
    this.backgroundButton = new JButton();
    this.foregroundButton = new JButton();
    this.helpButton = new JButton();
    this.cancelButton = new JButton();
    this.OKbutton = new JButton();

    setTitle("Style");
    setLocationByPlatform(true);
    setResizable(false);
    addWindowListener(new WindowAdapter() {

      public void windowClosed(WindowEvent evt) {
        StyleDialog.this.formWindowClosed(evt);
      }
    });
    addComponentListener(new ComponentAdapter() {

      public void componentHidden(ComponentEvent evt) {
        StyleDialog.this.formComponentHidden(evt);
      }

      public void componentShown(ComponentEvent evt) {
        StyleDialog.this.formComponentShown(evt);
      }
    });
    this.sampleTextPane.setEditable(false);
    this.sampleTextPane.setFocusable(false);
    this.sampleTextPane.setRequestFocusEnabled(false);
    this.jScrollPane2.setViewportView(this.sampleTextPane);

    this.jPanel2.setBorder(BorderFactory.createTitledBorder("Font"));

    this.fontSizeComboBox.setEditable(true);
    this.fontSizeComboBox.setModel(new DefaultComboBoxModel(new Integer[]{new Integer(3), new Integer(5), new Integer(6), new Integer(8), new Integer(11), new Integer(12), new Integer(14), new Integer(18), new Integer(24), new Integer(36), new Integer(48)}));
    this.fontSizeComboBox.setSelectedIndex(4);
    this.fontSizeComboBox.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent evt) {
        StyleDialog.this.fontSizeComboBoxActionPerformed(evt);
      }
    });
    this.fontNameComboBox.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent evt) {
        StyleDialog.this.fontNameComboBoxActionPerformed(evt);
      }
    });
    this.jLabel5.setText("Size:");

    this.jLabel3.setText("Name:");

    this.boldCheckBox.setText("Bold");
    this.boldCheckBox.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent evt) {
        StyleDialog.this.boldCheckBoxActionPerformed(evt);
      }
    });
    this.italicCheckBox.setText("Italic");
    this.italicCheckBox.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent evt) {
        StyleDialog.this.italicCheckBoxActionPerformed(evt);
      }
    });
    this.underlineCheckBox.setText("Underline");
    this.underlineCheckBox.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent evt) {
        StyleDialog.this.underlineCheckBoxActionPerformed(evt);
      }
    });
    this.strikeCheckBox.setText("Strike Through");
    this.strikeCheckBox.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent evt) {
        StyleDialog.this.strikeCheckBoxActionPerformed(evt);
      }
    });
    this.superscriptCheckBox.setText("Superscript");
    this.superscriptCheckBox.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent evt) {
        StyleDialog.this.superscriptCheckBoxActionPerformed(evt);
      }
    });
    this.subscriptCheckBox.setText("Subscript");
    this.subscriptCheckBox.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent evt) {
        StyleDialog.this.subscriptCheckBoxActionPerformed(evt);
      }
    });
    GroupLayout jPanel2Layout = new GroupLayout(this.jPanel2);
    this.jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.jLabel3).addComponent(this.fontNameComboBox, -2, 177, -2)).addGap(10, 10, 10).addGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.TRAILING).addGroup(jPanel2Layout.createSequentialGroup().addComponent(this.jLabel5).addGap(30, 30, 30)).addGroup(jPanel2Layout.createSequentialGroup().addComponent(this.fontSizeComboBox, -2, 43, -2).addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED))).addGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.boldCheckBox).addComponent(this.italicCheckBox)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.strikeCheckBox).addComponent(this.underlineCheckBox)).addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.superscriptCheckBox).addComponent(this.subscriptCheckBox))));

    jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jPanel2Layout.createSequentialGroup().addGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel3).addComponent(this.jLabel5)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.fontNameComboBox, -2, -1, -2).addComponent(this.fontSizeComboBox, -2, -1, -2).addComponent(this.boldCheckBox).addComponent(this.underlineCheckBox).addComponent(this.superscriptCheckBox)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.strikeCheckBox).addComponent(this.subscriptCheckBox).addComponent(this.italicCheckBox)).addContainerGap(8, 32767)));

    String[] fontlist = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
    this.fontNameComboBox.setModel(new DefaultComboBoxModel(fontlist));

    this.jPanel3.setBorder(BorderFactory.createTitledBorder("Color"));

    this.jLabel6.setText("Foreground:");

    this.jLabel7.setText("Background:");

    this.backgroundButton.setText("...");
    this.backgroundButton.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent evt) {
        StyleDialog.this.backgroundButtonActionPerformed(evt);
      }
    });
    this.foregroundButton.setText("...");
    this.foregroundButton.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent evt) {
        StyleDialog.this.foregroundButtonActionPerformed(evt);
      }
    });
    GroupLayout jPanel3Layout = new GroupLayout(this.jPanel3);
    this.jPanel3.setLayout(jPanel3Layout);
    jPanel3Layout.setHorizontalGroup(jPanel3Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jPanel3Layout.createSequentialGroup().addContainerGap().addGroup(jPanel3Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.jLabel6).addComponent(this.jLabel7)).addGap(4, 4, 4).addGroup(jPanel3Layout.createParallelGroup(GroupLayout.Alignment.LEADING, false).addComponent(this.foregroundButton, 0, 0, 32767).addComponent(this.backgroundButton, -2, 21, 32767)).addContainerGap(6, 32767)));

    jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jPanel3Layout.createSequentialGroup().addGroup(jPanel3Layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel6).addComponent(this.foregroundButton)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(jPanel3Layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel7).addComponent(this.backgroundButton))));

    this.helpButton.setText("Help");

    this.cancelButton.setText("Cancel");
    this.cancelButton.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent evt) {
        StyleDialog.this.cancelButtonActionPerformed(evt);
      }
    });
    this.OKbutton.setText("OK");
    this.OKbutton.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent evt) {
        StyleDialog.this.OKbuttonActionPerformed(evt);
      }
    });
    GroupLayout layout = new GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING).addGroup(layout.createSequentialGroup().addComponent(this.OKbutton, -2, 64, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.cancelButton).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.helpButton, -2, 63, -2)).addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false).addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup().addComponent(this.jScrollPane2, -2, 364, -2).addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(this.jPanel3, 0, 117, 32767)).addComponent(this.jPanel2, GroupLayout.Alignment.LEADING, -2, -1, -2))).addContainerGap(-1, 32767)));

    layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(this.jPanel2, -2, -1, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING).addComponent(this.jPanel3, -1, 92, 32767).addComponent(this.jScrollPane2, -1, 92, 32767)).addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.helpButton).addComponent(this.cancelButton).addComponent(this.OKbutton)).addGap(11, 11, 11)));

    pack();
  }

  private void fontSizeComboBoxActionPerformed(ActionEvent evt) {
    Integer integ = new Integer(Network.DEF_FONT_SIZE.intValue());
    try {
      integ = (Integer) this.fontSizeComboBox.getSelectedItem();
    } catch (ClassCastException e) {
      JOptionPane.showMessageDialog(this, "Numbers only. Cge man porn gd. Mao na!", "Error", 0, null);
    }
    if (integ.intValue() > Network.MAX_FONT_SIZE.intValue()) {
      integ = new Integer(Network.MAX_FONT_SIZE.intValue());
      JOptionPane.showMessageDialog(this, "Dakoa sad ana ui!", "Error", 0, null);
    } else if (integ.intValue() < Network.MIN_FONT_SIZE.intValue()) {
      integ = new Integer(Network.MIN_FONT_SIZE.intValue());
      JOptionPane.showMessageDialog(this, "Dli intawn na mabasa...", "Error", 0, null);
    }
    this.style.addAttribute(StyleConstants.FontSize, integ);
    updateSampleText(this.style);
  }

  private void fontNameComboBoxActionPerformed(ActionEvent evt) {
    this.style.addAttribute(StyleConstants.FontFamily, this.fontNameComboBox.getSelectedItem());
    updateSampleText(this.style);
  }

  private void boldCheckBoxActionPerformed(ActionEvent evt) {
    if (this.boldCheckBox.isSelected()) {
      this.style.addAttribute(StyleConstants.Bold, Boolean.valueOf(true));
    } else {
      this.style.removeAttribute(StyleConstants.Bold);
    }
    updateSampleText(this.style);
  }

  private void italicCheckBoxActionPerformed(ActionEvent evt) {
    if (this.italicCheckBox.isSelected()) {
      this.style.addAttribute(StyleConstants.Italic, Boolean.valueOf(true));
    } else {
      this.style.removeAttribute(StyleConstants.Italic);
    }
    updateSampleText(this.style);
  }

  private void underlineCheckBoxActionPerformed(ActionEvent evt) {
    if (this.underlineCheckBox.isSelected()) {
      this.style.addAttribute(StyleConstants.Underline, Boolean.valueOf(true));
    } else {
      this.style.removeAttribute(StyleConstants.Underline);
    }
    updateSampleText(this.style);
  }

  private void strikeCheckBoxActionPerformed(ActionEvent evt) {
    if (this.strikeCheckBox.isSelected()) {
      this.style.addAttribute(StyleConstants.StrikeThrough, Boolean.valueOf(true));
    } else {
      this.style.removeAttribute(StyleConstants.StrikeThrough);
    }
    updateSampleText(this.style);
  }

  private void superscriptCheckBoxActionPerformed(ActionEvent evt) {
    if (this.superscriptCheckBox.isSelected()) {
      this.style.addAttribute(StyleConstants.Superscript, Boolean.valueOf(true));
    } else {
      this.style.removeAttribute(StyleConstants.Superscript);
    }
    updateSampleText(this.style);
  }

  private void subscriptCheckBoxActionPerformed(ActionEvent evt) {
    if (this.subscriptCheckBox.isSelected()) {
      this.style.addAttribute(StyleConstants.Subscript, Boolean.valueOf(true));
    } else {
      this.style.removeAttribute(StyleConstants.Subscript);
    }
    updateSampleText(this.style);
  }

  private void backgroundButtonActionPerformed(ActionEvent evt) {
    Color background = JColorChooser.showDialog(this, "Pick background color", this.initialBackground);
    if (background != null) {
      this.style.addAttribute(StyleConstants.Background, background);
    } else {
      this.style.removeAttribute(StyleConstants.Background);
    }
    updateSampleText(this.style);
  }

  private void foregroundButtonActionPerformed(ActionEvent evt) {
    Color foreground = JColorChooser.showDialog(this, "Pick foreground color", this.initialForeground);
    if (foreground != null) {
      this.style.addAttribute(StyleConstants.Foreground, foreground);
    } else {
      this.style.removeAttribute(StyleConstants.Foreground);
    }
    updateSampleText(this.style);
  }

  private void formComponentShown(ComponentEvent evt) {
    this.style = this.chatClient.getCurrentStyle();
    updateControls(this.style);
    updateSampleText(this.style);
    this.previousStyle = ((Style) this.style.copyAttributes());
  }

  private void OKbuttonActionPerformed(ActionEvent evt) {
    this.commit = Boolean.valueOf(true);
    setVisible(false);
  }

  private void cancelButtonActionPerformed(ActionEvent evt) {
    this.commit = Boolean.valueOf(false);
    setVisible(false);
  }

  private void formComponentHidden(ComponentEvent evt) {
    if (this.commit.booleanValue()) {
      this.chatClient.styleChangeCommit(this.style);
    } else {
      this.chatClient.styleChangeCommit(this.previousStyle);
    }
  }

  private void formWindowClosed(WindowEvent evt) {
    this.chatClient.styleChangeCommit(this.previousStyle);
  }

  private void updateSampleText(Style style) {
    try {
      this.sample.remove(0, this.sample.getLength());
      this.sample.insertString(0, "The quick brown fox jumps over the lazy dog.", style);
    } catch (BadLocationException ex) {
      Logger.getLogger(StyleDialog.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  private void updateControls(Style style) {
    this.fontNameComboBox.setSelectedItem(StyleConstants.getFontFamily(style));
    this.fontSizeComboBox.setSelectedItem(Integer.valueOf(StyleConstants.getFontSize(style)));
    this.boldCheckBox.setSelected(StyleConstants.isBold(style));
    this.italicCheckBox.setSelected(StyleConstants.isItalic(style));
    this.underlineCheckBox.setSelected(StyleConstants.isUnderline(style));
    this.strikeCheckBox.setSelected(StyleConstants.isStrikeThrough(style));
    this.subscriptCheckBox.setSelected(StyleConstants.isSubscript(style));
    this.superscriptCheckBox.setSelected(StyleConstants.isSuperscript(style));
    this.initialForeground = StyleConstants.getForeground(style);
    this.initialBackground = StyleConstants.getBackground(style);
  }
}